package com.ruoyi.web.util;

import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;

import java.io.IOException;

@Configuration
public class AutoBrowser {
    @EventListener(ApplicationReadyEvent.class)
    public void applicationReadyEvent() {
        String url = "http://localhost:80";
        System.out.println("应用已经准备就绪 ... 启动浏览器，URL：" + url);

        Runtime runtime = Runtime.getRuntime();
        String os = System.getProperty("os.name").toLowerCase();

        try {
            if (os.contains("win")) {
                // Windows
                runtime.exec("rundll32 url.dll,FileProtocolHandler " + url);
            } else{
                runtime.exec(new String[]{"open", url});
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
